package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportPurchaseDoc;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.TransportPurchaseDocDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2022-01-28 11:17:06
 */
public class TransportPurchaseDocConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportPurchaseDocConverts.class);

    public static List<TransportPurchaseDocDTO> convert(List<TransportPurchaseDoc> transportPurchaseDocList) {
        if (CollectionUtils.isEmpty(transportPurchaseDocList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportPurchaseDocDTO> transportPurchaseDocListResult = Lists.newArrayList();
        for (TransportPurchaseDoc transportPurchaseDoc : transportPurchaseDocList) {
            transportPurchaseDocListResult.add(convert(transportPurchaseDoc));
        }
        return transportPurchaseDocListResult;
    }

    public static TransportPurchaseDocDTO convert(TransportPurchaseDoc transportPurchaseDoc) {
        try {
            return BeanUtils.copy(transportPurchaseDoc, TransportPurchaseDocDTO.class);
        } catch (Exception e) {
            LOG.error("TransportPurchaseDoc到TransportPurchaseDocDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportPurchaseDoc到TransportPurchaseDocDTO转换失败", e);
        }
    }

    public static TransportPurchaseDoc convert(TransportPurchaseDocDTO transportPurchaseDocDTO) {
        try {
            return BeanUtils.copy(transportPurchaseDocDTO, TransportPurchaseDoc.class);
        } catch (Exception e) {
            LOG.error("TransportPurchaseDocDTO到TransportPurchaseDoc转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportPurchaseDocDTO到TransportPurchaseDoc转换失败", e);
        }
    }
}